package demo;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class AirTicketCalculator {
    
    // 计算无尾差的航空票务金额
    public static void calculateTicketAmounts(double ticketPrice, double fuelSurcharge, double taxRate) {
        // 使用BigDecimal避免浮点数精度问题
        BigDecimal price = BigDecimal.valueOf(ticketPrice);
        BigDecimal fuel = BigDecimal.valueOf(fuelSurcharge);
        BigDecimal rate = BigDecimal.valueOf(taxRate);
        
        // 计算不含税总金额（票面价 + 燃油附加费）
        BigDecimal totalExcludingTax = price.add(fuel);
        
        // 计算增值税税额 = 不含税总金额 * 税率 / (1 + 税率)
        // 这样可以确保 不含税金额 + 税额 = 含税金额 完全匹配
        BigDecimal taxAmount = totalExcludingTax.multiply(rate)
                .divide(rate.add(BigDecimal.ONE), 2, RoundingMode.HALF_UP);
        
        // 计算含税总金额
        BigDecimal totalIncludingTax = totalExcludingTax.add(taxAmount);
        
        // 输出结果
        System.out.println("票面价: " + price.setScale(2, RoundingMode.HALF_UP));
        System.out.println("燃油附加费: " + fuel.setScale(2, RoundingMode.HALF_UP));
        System.out.println("不含税金额: " + totalExcludingTax.setScale(2, RoundingMode.HALF_UP));
        System.out.println("增值税税率: " + rate.multiply(BigDecimal.valueOf(100)).setScale(0) + "%");
        System.out.println("增值税税额: " + taxAmount.setScale(2, RoundingMode.HALF_UP));
        System.out.println("含税总金额: " + totalIncludingTax.setScale(2, RoundingMode.HALF_UP));
    }
    
    public static void main(String[] args) {
        // 示例数据
        double ticketPrice = 1522.94;
        double fuelSurcharge = 18.35;
        double taxRate = 0.09; // 9%
        
        calculateTicketAmounts(ticketPrice, fuelSurcharge, taxRate);
    }
}